package com.zatech.cgnci.project.controller.claim;

import com.alibaba.fastjson.JSON;
import com.zatech.cgnci.project.dto.CgnciClaimCaseAddDTO;
import com.zatech.cgnci.project.dto.CgnciClaimCaseCheckDTO;
import com.zatech.cgnci.project.dto.CgnciClaimCaseReopenDTO;
import com.zatech.cgnci.project.mapper.CgnciApprovalInfoMapper;
import com.zatech.cgnci.project.service.CgnciClaimCaseService;
import com.zatech.cgnci.project.service.CgnciClaimCaseTaskService;
import com.zatech.cgnci.project.service.CgnciReinsurancePremiumBillService;
import com.zatech.cgnci.project.service.IApprovalService;
import com.zatech.cgnci.project.vo.ApprovalConfigVO;
import com.zatech.cgnci.project.vo.CgnciClaimCaseCreateResultVO;
import com.zatech.cgnci.project.vo.CgnciClaimCaseReopenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 理赔案件主表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2024-12-23
 */
@RestController
@RequestMapping("/api/claim/caseTask")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "理赔案件管理接口")
@Slf4j
public class CgnciClaimCaseTaskController {
    private final CgnciClaimCaseService cgnciClaimCaseService;
    private final CgnciClaimCaseTaskService cgnciClaimCaseTaskService;
    private final IApprovalService approvalService;
    private final CgnciApprovalInfoMapper approvalInfoMapper;
    private final CgnciReinsurancePremiumBillService premiumBillService;


    @GetMapping("/caseProcessCheck")
    @ApiOperation(value = "立案处理检查")
    @Transactional(rollbackFor = Exception.class)
    public void caseProcessCheck(@RequestParam("caseNo") String caseNo) {
        Assert.hasText(caseNo, "案件号必填.");
        cgnciClaimCaseTaskService.caseProcessCheck(caseNo);
    }

    @PostMapping("/caseAfterSave")
    @ApiOperation(value = "立案后更新保存")
    @Transactional(rollbackFor = Exception.class)
    public void caseAfterSave(@RequestBody @Valid CgnciClaimCaseAddDTO req) {
        log.info("{}立案后更新保存{}", req.getCaseNo(), JSON.toJSONString(req));
        Assert.notNull(req.getCaseId(), "案件id必填.");
        Assert.hasText(req.getCaseNo(), "案件号必填.");
        Assert.hasText(req.getSaveType(), "保存类型必填。");
        cgnciClaimCaseTaskService.caseProcessCheck(req.getCaseNo());
        cgnciClaimCaseTaskService.caseAfterSave(req);
    }

    @GetMapping("/createCase")
    @ApiOperation(value = "新增立案")
    public CgnciClaimCaseCreateResultVO createCase(@RequestParam("policyNo") String policyNo,
                                                   @RequestParam(value = "confirm", required = false) Boolean confirm) {return cgnciClaimCaseTaskService.createCase(policyNo, confirm);
    }

    @PostMapping("/saveCase")
    @ApiOperation(value = "保存立案件")
    @Transactional(rollbackFor = Exception.class)
    public void saveCase(@RequestBody @Valid CgnciClaimCaseAddDTO req) {
        Assert.hasText(req.getTaskNo(), "案件任务号必填.");
        Assert.hasText(req.getCaseNo(), "案件号必填.");
        log.info("{}保存立案件{}", req.getTaskNo(), JSON.toJSONString(req));
        cgnciClaimCaseTaskService.saveClaimCaseTask(req);
    }


    @GetMapping("/submitCase")
    @ApiOperation(value = "提交立案")
    @Transactional(rollbackFor = Exception.class)
    public void submitCase(@RequestParam("taskNo") String taskNo) {
        Assert.hasText(taskNo, "案件任务号必填.");
        cgnciClaimCaseTaskService.submitCase(taskNo);
    }

    @PostMapping("/approveCase")
    @ApiOperation(value = "立案审核同意")
    @Transactional(rollbackFor = Exception.class)
    public void approveCase(@RequestBody CgnciClaimCaseCheckDTO req) {
        Assert.hasText(req.getTaskNo(), "案件任务号必填.");
        cgnciClaimCaseTaskService.approveCase(req);
    }

    @GetMapping("/listBackNode")
    @ApiOperation(value = "获取退回流程节点")
    public List<ApprovalConfigVO> listBackNode(@RequestParam(value = "taskNo") String taskNo) {
        return cgnciClaimCaseTaskService.listBackNode(taskNo);
    }

    @PostMapping("/backCase")
    @ApiOperation(value = "立案退回")
    @Transactional(rollbackFor = Exception.class)
    public void backCase(@RequestBody CgnciClaimCaseCheckDTO req) {
        Assert.hasText(req.getTaskNo(), "案件任务号必填.");
        Assert.notNull(req.getApprovalNode(), "退回节点必填。");
        log.info("{}立案退回:{}", req.getTaskNo(), JSON.toJSONString(req));
        cgnciClaimCaseTaskService.backCase(req);
    }


    @GetMapping("/logOffCase")
    @ApiOperation(value = "立案注销")
    @Transactional(rollbackFor = Exception.class)
    public void logOffCase(@RequestParam("taskNo") String taskNo) {
        Assert.hasText(taskNo, "任务号必填。");
        cgnciClaimCaseTaskService.logOffCase(taskNo);
    }

    @PostMapping("/reopenCase")
    @ApiOperation(value = "申请重开案件")
    @Transactional(rollbackFor = Exception.class)
    public CgnciClaimCaseReopenVO reopenCase(@RequestBody CgnciClaimCaseReopenDTO req) {
        Assert.hasText(req.getCaseNo(), "案件号必填。");
        log.info("{}申请重开案件:{}", req.getCaseNo(), JSON.toJSONString(req));
        return cgnciClaimCaseTaskService.reopenCase(req);
    }

    @GetMapping("/submitReopenCase")
    @ApiOperation(value = "申请重开提交")
    @Transactional(rollbackFor = Exception.class)
    public void submitReopenCase(@RequestParam("taskNo") String taskNo) {
        Assert.hasText(taskNo, "案件任务号必填.");
        cgnciClaimCaseTaskService.submitReopenCase(taskNo);
    }

    @PostMapping("/approveReopenCase")
    @ApiOperation(value = "申请重开案件-审核同意")
    @Transactional(rollbackFor = Exception.class)
    public void approveReopenCase(@RequestBody CgnciClaimCaseReopenDTO req) {
        Assert.hasText(req.getTaskNo(), "案件任务号必填。");
        cgnciClaimCaseTaskService.approveReopenCase(req);
    }

    @GetMapping("/logOffReopenCase")
    @ApiOperation(value = "重开注销")
    @Transactional(rollbackFor = Exception.class)
    public void logOffReopenCase(@RequestParam("taskNo") String taskNo) {
        Assert.hasText(taskNo, "任务号必填。");
        cgnciClaimCaseTaskService.logOffReopenCase(taskNo);
    }
//    public static void main(String[] args) {
//        String s = JSON.toJSONString(new CgnciClaimCaseAddDTO(), SerializerFeature.WriteNullStringAsEmpty);
//        System.out.println("格式：" + s);
//    }

    @GetMapping("/verifyPremium")
    @ApiOperation(value = "保费实收校验")
    public Boolean verifyPremium(@RequestParam("policyNo") String policyNo) {
        Assert.hasText(policyNo, "保单号必填。");
        return premiumBillService.verifyPremium(policyNo);
    }
}